﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Text;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using QFTMCO;
using INTEX.DataBase;
namespace WPFExcelReport
{

    public partial class QFTMCOData
    {

        #region=============Cost===============
        //KAAUF,KAFOLGE,KAREZEPT,KASTBAT,KASTGA,KASTKE
        //KAAART,KAAVAR,KAAFARBE,KAABESCHI
        public SETINTEXDB SETINTEXDB = new SETINTEXDB();
        int LastStage = 9;
        DataTable tbMaster
        {
            get
            {
                return SETINTEXDB.QUTMASTER;
            }
        }
        public DataSet LoadKAFOLGE(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KAFOLGE WHERE  KAFKANR = :KAFKANR AND KAFKAPOS=:KAFKAPOS"
              + " order by 1,2,3,4,5,6,11,12,13 desc";
                or1.Parameters.Add("KAFKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KAFKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KAFOLGE";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKAREZEPT(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                //KAAKANR ,KAAKAPOS
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KAREZEPT WHERE  KARKANR = :KARKANR AND KARKAPOS=:KARKAPOS"
               + " order by 1 desc";
                or1.Parameters.Add("KARKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KARKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KAREZEPT";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKASTBAT(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                //KAAKANR ,KAAKAPOS
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KASTBAT WHERE  KBAKANR = :KBAKANR AND KBAKAPOS=:KBAKAPOS"
             + " order by 1 desc";
                or1.Parameters.Add("KBAKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KBAKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KASTBAT";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKASTGA(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                //KAAKANR ,KAAKAPOS
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KASTGA WHERE  KGAKANR = :KGAKANR AND KGAKAPOS=:KGAKAPOS"
              + " order by 1,2,3,4,5,6,7 desc";
                or1.Parameters.Add("KGAKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KGAKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KASTGA";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKASTKE(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                //KAAKANR ,KAAKAPOS
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KASTKE WHERE  KKEKANR = :KKEKANR AND KKEKAPOS=:KKEKAPOS "
                + " order by 1 desc";
                or1.Parameters.Add("KKEKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KKEKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KASTKE";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKAVERF(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                //KAAKANR ,KAAKAPOS
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KAVERF WHERE  KAVKANR = :KAVKANR AND KAVKAPOS=:KAVKAPOS "
                + " order by 1 desc";
                or1.Parameters.Add("KAVKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KAVKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KAVERF";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKAAUF(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string KAAART = dwInput["KAAART"].ToString(); ;
                string KAAVAR = dwInput["KAAVAR"].ToString(); ;
                string KAAFARBE = dwInput["KAAFARBE"].ToString(); ;
                string KAABESCHI = dwInput["KAABESCHI"].ToString(); ;

                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KAAUF "
                + " WHERE  KAAART = '" + KAAART + "' AND KAAVAR='" + KAAVAR + "' AND KAAFARBE='" + KAAFARBE
                + "' AND KAABESCHI='" + KAABESCHI + "'"
               + " order by 1 desc";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KAAUF";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet ListQuatation(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwART = null;
            try
            {
                setResult = LoadKAAUF(dwInput);
                setReturn.Merge(setResult);
                foreach (DataRow dwTemp in setReturn.Tables["KAAUF"].Rows)
                {

                    setResult = LoadKAFOLGE(dwTemp);
                    setReturn.Merge(setResult);
                    setResult = LoadKAREZEPT(dwTemp);
                    setReturn.Merge(setResult);
                    setResult = LoadKASTBAT(dwTemp);
                    setReturn.Merge(setResult);
                    setResult = LoadKASTGA(dwTemp);
                    setReturn.Merge(setResult);
                    setResult = LoadKASTKE(dwTemp);
                    setReturn.Merge(setResult);
                    setResult = LoadKAVERF(dwTemp);
                    setReturn.Merge(setResult);

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet GetYarnPrice(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tbLocal = new DataTable();
            try
            {
                string art = dwInput["QUARTICLE"].ToString();
                string var = dwInput["QUVAR"].ToString();
                string color = dwInput["QUCOLOR"].ToString();
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT ROWNUM AS YID,'0' AS YSUPID,'HLT' AS YSUPNAME,GWBART AS YYARN,GWBVAR AS YVAR,GWBFARBE AS YCOLOR"
                    + ", GWPRVH*0.01 AS YPPRICE,GWPRAK*0.01 AS YCPRICE,GWPRKU*0.01 AS YFPRICE,'KG' AS YUOM, GWAENDDAT AS YEFFECTDATE,"
                    + "GWNEUBEN AS YUSER, GWNEUDAT AS YDATE,"
                    + "'' AS YREMARK1,'' AS YREMARK2,'' AS  YREMARK3"
                    + " FROM GWERT WHERE   GWPART='EP   ' AND GWBART=:GWBART";
                or1.Parameters.Add("GWBART", OracleType.NVarChar).Value = art;
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KAAUF";
                    tbLocal = setResult.Tables[0];
                }
                if (tbLocal.Rows.Count == 0)
                    return setReturn;
                string cond = "YYARN='" + art + "' and YVAR='" + var + "' and YCOLOR='" + color + "'";
                DataRow[] rows = tbLocal.Select(cond);
                DataTable yarnp = new DataTable();
                if (rows.Length > 0)
                {
                    yarnp = rows.CopyToDataTable();
                    yarnp.TableName = "KAAUF";
                }
                setReturn.Merge(yarnp);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        #endregion

        int maxid = 1;
        public Int64 GetQUID()
        {
            Int64 result = maxid++;
            try
            {
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public DataSet BuidProcessCost(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tbMaster = SETINTEXDB.Tables["QUTMASTER"];
            DataRow[] rows = null;
            string cond = "PSTAGE='";
            try
            {
                int stage = 0;
                DataRow dwProd = null;
                int id = 1;
                ArrayList Listprocess = new ArrayList();
                foreach (DataRow dwMaster in tbMaster.Rows)
                {
                    NewProdRow(dwMaster);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        private void NewProdRow(DataRow dwMaster)
        {
            try
            {
                string order = dwMaster["QUORDER"].ToString();
                string pos = dwMaster["QULINE"].ToString();
                string KRS = order.Substring(0, 2);
                string LFD = Right(order, 6);
                string expression = "";
                DataRow[] rows = null;
                DataRow[] rows1 = null;
                expression = "ARAUFKRS='" + KRS + "' AND ARAUFLFD='" + LFD + "' AND ARAUFPOS='" + pos + "'";
                rows = SETINTEXDB.ABPR.Select(expression);
                if (rows.Length == 0)
                    return;
                foreach (DataRow row in rows)
                {
                    string step = row["ARAFOLGE"].ToString();
                    double price = System.Convert.ToDouble(row["ARWERT"].ToString()) * 0.001;
                    DataRow dwProce = SETINTEXDB.QUPROCESS.NewRow();
                    dwProce["QPID"] = GetQUID();
                    dwProce["QPORDER"] = dwMaster["QUORDER"];
                    dwProce["QPLINE"] = dwMaster["QULINE"];
                    dwProce["QPPOS"] = step;
                    dwProce["QPSTAGE"] = dwMaster["QUSTAGE"];
                    dwProce["QPPROCESS"] = dwMaster["QUSTAGEDES"];
                    dwProce["QPPROCDES"] = dwMaster["QUSTAGEDES"];
                    dwProce["QPMASTERID"] = dwMaster["QUBOMID"].ToString();
                    dwProce["QPQUANTITY"] = dwMaster["QUCQUANTITY"];
                    dwProce["QPUOM"] = dwMaster["QUUOM"];
                    dwProce["QPCVALUE"] = 0;
                    dwProce["QPCPRICE"] = price;
                    dwProce["QPRATIO"] = 1;
                    dwProce["QPUSER"] = UserID;
                    dwProce["QPDATE"] = iDate;
                    if (step != "000")
                    {
                        expression = "ABFAUFKRS='" + KRS + "' AND ABFAUFLFD='" + LFD + "' AND ABFAUFPOS='" + pos + "' AND ABFAFOLGE='" + step + "'";
                        rows1 = SETINTEXDB.ABFOLGE.Select(expression);
                        if (rows1.Length > 0)
                        {
                            dwProce["QPPROCESS"] = rows1[0]["ABFAGANG"].ToString();
                            dwProce["QPPROCDES"] = rows1[0]["ABFAGANG"].ToString();
                        }
                    }
                    SETINTEXDB.QUPROCESS.Rows.Add(dwProce);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public DataSet BuidMatBOMCost(DataRow dwInput)
        {
            DataSet Return = new DataSet();
            DataSet Result = new DataSet();
            string cond = "", vart = "", vvar = "", vcolor = "", vdess = "", vcolorit = "", vfinish = "", order = "", line = "";
            string abtype = "";
            string articletype = "";
            double qty = 0.0;
            double bomratio = 0.0;
            DataRow[] rows = null;
            string expression = "";

            int laststage = 9;
            try
            {
                CreateSalesCost();

                foreach (DataRow row in SETINTEXDB.ABPOS.Rows)
                {
                    abtype = row["ABPAUFART"].ToString();
                    articletype = row["ABPWAKZ"].ToString();
                    vart = row["ABPART"].ToString();
                    vvar = row["ABPVAR"].ToString();
                    vcolor = row["ABPFARBE"].ToString();
                    vdess = row["ABPDESS"].ToString();
                    vcolorit = row["ABPCOLORIT"].ToString();
                    vfinish = row["ABPBESCHICH"].ToString();
                    if (abtype == "VA"
                        && articletype == "F")
                    {
                        CreateFinishCost(row);
                        continue;
                    }
                    if (abtype == "VA"
                        && articletype == "R"
                        && vdess.Trim() != ""
                        )
                    {
                        CreateGPrintingCost(row);
                        continue;
                    }
                    if (abtype == "VA"
                        && articletype == "R"
                        && vdess.Trim() == ""
                        )
                    {
                        CreateGDyeingCost(row);
                        continue;
                    }
                    if (abtype == "PR")
                    {
                        CreateGreigeCost(row);
                        continue;
                    }
                    if (abtype == "GP")
                    {
                        CreateDyedYarnCost(row);
                        continue;
                    }
                }
                //GetRaw Yarn Cost
                DataRow rowy = null;
                foreach (DataRow row in SETINTEXDB.GAAUF.Rows)
                {
                    string gasa = row["GASA"].ToString();
                    if (gasa == "713") continue;
                    string yart = row["GAART"].ToString();
                    string yvar = row["GAVAR"].ToString();
                    string ycolor = row["GAFARBE"].ToString();
                    expression = "EKPART='" + yart + "' AND EKPVAR='" + yvar + "' AND EKPFARBE='" + ycolor + "'";
                    rows = SETINTEXDB.EKPOS.Select(expression);
                    if (rows.Length == 0)
                        rowy = null;
                    else
                    {
                        rowy = rows[0];
                    }
                    if (gasa == "310")
                    {
                        CreateMatPrice(row, rowy, "Yarn");
                    }
                    if (gasa == "733")
                    {
                        CreateMatPrice(row, rowy, "Greige");
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void RefreshCost()
        {
            try
            {
                GetStageCost("21");
                GetStageCost("5");
                GetStageCost("6");
                GetStageCost("7");
                GetStageCost("8");
                GetStageCost("9");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void PublishCost(DataRow row)
        {
            var query = from rq in SETINTEXDB.QUTMASTER.AsEnumerable()
                        where rq["QUARTICLE"].ToString() == row["QUARTICLE"].ToString()
                        & rq["QUVAR"].ToString() == row["QUVAR"].ToString()
                        & rq["QUCOLOR"].ToString() == row["QUCOLOR"].ToString()
                        & rq["QUDESS"].ToString() == row["QUDESS"].ToString()
                        & rq["QUCOLORIT"].ToString() == row["QUCOLORIT"].ToString()
                        & rq["QUFINISH"].ToString() == row["QUFINISH"].ToString()
                        select rq;
            foreach (DataRow rq in query)
            {
                double mqty = System.Convert.ToDouble(rq["QUCQUANTITY"].ToString());
                double mprice = System.Convert.ToDouble(row["QUCPRICE"].ToString());
                rq["QUCPRICE"] = mprice;
                rq["QUCVALUE"] = mqty * mprice;
            }
        }
        private void GetStageCost(string stage)
        {

            try
            {
                double mcost = 0;
                double pcost = 0;
                double aqty = 1;
                var querya = from rm in SETINTEXDB.QUTMASTER.AsEnumerable()
                             where rm["QUSTAGE"].ToString() == stage
                             select rm;
                //建立原料需求行，而单价从生产单过来。在这里匹配单价。为GAAUF是色纱的找到对应的行。
                foreach (DataRow row in querya)
                {

                    #region======Sum Material cost=======
                    string bomid = row["QUBOMID"].ToString();
                    aqty = System.Convert.ToDouble(row["QUCQUANTITY"].ToString());
                    if (aqty == 0)
                        aqty = 1;
                    var querym = from rm in SETINTEXDB.QUTMASTER.AsEnumerable()
                                 where rm["QUSBOMID"].ToString() == bomid
                                 select rm;
                    foreach (DataRow rm in querym)
                    {
                        double mprice = System.Convert.ToDouble(rm["QUCPRICE"].ToString());
                        double mqty = System.Convert.ToDouble(rm["QUCQUANTITY"].ToString());
                        double cvalue = mprice * mqty;
                        rm["QUCVALUE"] = cvalue;
                        mcost += cvalue;
                    }
                    #endregion

                    #region======Sum Process cost=======
                    var queryp = from rm in SETINTEXDB.QUPROCESS.AsEnumerable()
                                 where rm["QPMASTERID"].ToString() == bomid
                                 select rm;
                    foreach (DataRow rm in queryp)
                    {
                        double pprice = System.Convert.ToDouble(rm["QPCPRICE"].ToString());
                        double pqty = System.Convert.ToDouble(rm["QPQUANTITY"].ToString());
                        double pvalue = pprice * pqty;
                        rm["QPCVALUE"] = pvalue;
                        pcost += pvalue;
                    }
                    row["QUCVALUE"] = mcost + pcost;
                    row["QUCPRICE"] = (mcost + pcost) / aqty;
                    #endregion
                    PublishCost(row);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void CreateSalesCost()
        {
            string cond = "", vart = "", vvar = "", vcolor = "", vdess = "", vcolorit = "", vfinish = "", order = "", line = "";
            double qty = 0.0;
            double bomratio = 0.0;
            string expression = "";
            DataRow[] rows = null;
            DataRow[] rows1 = null;
            double fnratio = 1;
            string key = order + line;
            string KRS = "", LFD = "", POS = "";
            rows = SETINTEXDB.VKPOS.Select();
            if (rows.Length == 0)
                return;
            LastStage = 9;
            foreach (DataRow row in rows)
            {
                KRS = row["VKPAUFKRS"].ToString();
                LFD = row["VKPAUFLFD"].ToString();
                POS = row["VKPAUFPOS"].ToString();
                key = "VK" + KRS + LFD + POS;
                qty = System.Convert.ToDouble(row["VKPMENGE1"].ToString()) * 0.01;
                expression = "WDSA='K' AND WDWAKZ='F' AND WDAUFKRS='" + KRS + "' AND WDAUFLFD='" + LFD + "' AND WDAUFPOS='" + POS + "'";
                rows1 = SETINTEXDB.WDECK.Select(expression);
                if (rows1.Length == 0)
                    continue;
                DataRow row1 = rows1[0];
                vart = row1["WDART"].ToString();
                vvar = row1["WDVAR"].ToString();
                vcolor = row1["WDFARBE"].ToString();
                vdess = row1["WDDESS"].ToString();
                vcolorit = row1["WDCOLORIT"].ToString();
                vfinish = row1["WDBESCHICH"].ToString();
                DataRow dwCO = tbMaster.NewRow();
                dwCO["QUUSER"] = UserID;
                dwCO["QUDATE"] = iDate;
                dwCO["QUID"] = GetQUID();
                dwCO["QUORDER"] = KRS + LFD;
                dwCO["QULINE"] = POS;
                dwCO["QUSTAGE"] = 9;
                dwCO["QUSTAGEDES"] = "分摊";
                dwCO["QUCVALUE"] = 0;
                dwCO["QUSOURCETYPE"] = "I";
                //自己的ID（BOMID），上一级的ID（SBOMID）
                dwCO["QUBOMID"] = "9." + key;
                dwCO["QUSBOMID"] = key;
                dwCO["QUARTICLE"] = vart;
                dwCO["QUVAR"] = vvar;
                dwCO["QUCOLOR"] = vcolor;
                dwCO["QUDESS"] = vdess;
                dwCO["QUCOLORIT"] = vcolorit;
                dwCO["QUFINISH"] = vfinish;
                dwCO["QUCPRICE"] = 0;               
                dwCO["QUCQUANTITY"] = qty;
                dwCO["QURATIO"] = fnratio;
                tbMaster.Rows.Add(dwCO);
            }


            /*
             1.建立BOM关系，分工艺标准，计划和实际
             2.通过物料关系建立BOM关系，每一级别有自己的BOMID，以便下一级别能够找到“目标”
             3.每个级别可能有多个生产单，从数量上，汇总这些单的数量，作为这一级别的计划数量或实际数量
             4.每个级别通过SBOMID向上一级别传递数量和价值，如果这一个级别是上面多个级别的合并，则需要为每个级别建立一个记录，区分开需求
             * 1. 本级数量，本级价格，本级价值。本级别对应的生产单s，对应的生产计划数量，实际数量。
             * 2.本级别都进行了哪些加工？从生产单进行提取。
             * 3. 贡献给上一级别的价值。从BOM角度，获取标准的数量需求。关系到数量和价值。
             * 4.如果是一个生产单，那么这个生产单的价值就是需要的价值。如果有多个需求点，则需要按照需求点进行拆分。有实际的生成单计划数量，如果这个生产单有，获得单价。比如需要200kg，实际可能消耗了210kg。可能有合并生产单，
             * 5.以生产单为主线的价值评估。每个生产单有自己的价值。这个容易计算。
             * 6.以销售单为主线。要分清楚"从共享的单上，需要多少？"从自己的单获得理论消耗量，计划消耗量和实际消耗量。单价只有一个。按照生产单来。
             */
        }
        private void CreateFinishCost(DataRow dwInput)
        {
            string vart = "", vvar = "", vcolor = "", vdess = "", vcolorit = "", vfinish = "", order = "", line = "";
            double qty = 0.0;
            double bomratio = 0.0;
            string expression = "";
            DataRow[] rows = null;
            DataRow[] rows1 = null;
            double fnratio = 1;
            string key = order + line;
            string KRS = "", LFD = "", POS = "";

            //找到对应的后整理，获得实际数量，计划数量

            //double reqqty = System.Convert.ToDouble(dwInput["QUCQUANTITY"].ToString());
            //假设一对一

            KRS = dwInput["ABPAUFKRS"].ToString();
            LFD = dwInput["ABPAUFLFD"].ToString();
            POS = dwInput["ABPAUFPOS"].ToString();
            key = "AB" + KRS + LFD + POS;
            order = KRS + LFD;
            qty = System.Convert.ToDouble(dwInput["ABPEAKG"].ToString()) * 0.01;
            vart = dwInput["ABPART"].ToString();
            vvar = dwInput["ABPVAR"].ToString();
            vcolor = dwInput["ABPFARBE"].ToString();
            vdess = dwInput["ABPDESS"].ToString();
            vcolorit = dwInput["ABPCOLORIT"].ToString();
            vfinish = dwInput["ABPBESCHICH"].ToString();
            DataRow dwCO = tbMaster.NewRow();
            dwCO["QUUSER"] = UserID;
            dwCO["QUDATE"] = iDate;
            dwCO["QUID"] = key;
            dwCO["QUORDER"] = order;
            dwCO["QULINE"] = POS;
            dwCO["QUSTAGE"] = 8;
            dwCO["QUSTAGEDES"] = "整理";
            dwCO["QUSOURCETYPE"] = "I";
            //自己的ID（BOMID），上一级的ID（SBOMID）
            dwCO["QUBOMID"] = "8." + key;
            dwCO["QUSBOMID"] = "9." + key;//目标指向销售单行需求，需要分配对应多行问题。
            dwCO["QUARTICLE"] = vart;
            dwCO["QUVAR"] = vvar;
            dwCO["QUCOLOR"] = vcolor;
            dwCO["QUDESS"] = vdess;
            dwCO["QUCOLORIT"] = vcolorit;
            dwCO["QUFINISH"] = vfinish;
            // QUDESS QUCOLORIT QUFINISH
            dwCO["QUCPRICE"] = 0;
            dwCO["QUCQUANTITY"] = qty;
            dwCO["QUCVALUE"] = 0;
            tbMaster.Rows.Add(dwCO);
            /*  var query = from dw in SETINTEXDB.WBBEW.AsEnumerable()
                          where (dw["WBEWBS"].ToString() == "48" || dw["WBEWBS"].ToString() == "36")
                          & dw["WBEWAUFKRS"].ToString() == KRS
                          & dw["WBEWAUFLFD"].ToString() == LFD
                          & dw["WBEWAUFPOS"].ToString() == POS
                          group dw by new
                          {
                              WBEWART = dw["WBEWART"],
                              WBEWVAR = dw["WBEWVAR"],
                              WBEWDESS = dw["WBEWDESS"],
                              WBEWCOLORIT = dw["WBEWCOLORIT"],
                              WBEWFARBE = dw["WBEWFARBE"],
                              WBEWBESCHICH = dw["WBEWBESCHICH"]
                          }
                              into g
                              select new
                              {
                                  Fabric = g.Key,
                                  TWeight = g.Sum(dw => dw.Field<double>("WBEWGEW")),
                              
                              };*/

            var query = from dw in SETINTEXDB.WBBEW.AsEnumerable()
                        where (dw["WBEWBS"].ToString() == "48" || dw["WBEWBS"].ToString() == "36")
                        & dw["WBEWAUFKRS"].ToString() == KRS
                        & dw["WBEWAUFLFD"].ToString() == LFD
                        & dw["WBEWAUFPOS"].ToString() == POS
                        select dw;
            string processed = "";

            string gkey = key;
            foreach (var row in query)
            {
                string yart = row["WBEWART"].ToString();
                string yvar = row["WBEWVAR"].ToString();
                string ydess = row["WBEWDESS"].ToString();
                string ycolorway = row["WBEWCOLORIT"].ToString();
                string ycolor = row["WBEWFARBE"].ToString();
                string yfinish = row["WBEWBESCHICH"].ToString();
                double yqty = System.Convert.ToDouble(row["WBEWGEW"].ToString()) * 0.001; //   取留位数量

                dwCO = tbMaster.NewRow();
                dwCO["QUUSER"] = UserID;
                dwCO["QUDATE"] = iDate;
                dwCO["QUID"] = GetQUID();
                dwCO["QUORDER"] = order;
                dwCO["QULINE"] = line;
                dwCO["QUSTAGE"] = 8;
                dwCO["QUSTAGEDES"] = "坯布面料";
                dwCO["QUSOURCETYPE"] = "I";
                //自己的ID（BOMID），上一级的ID（SBOMID）
                dwCO["QUBOMID"] = "8." + gkey;
                dwCO["QUSBOMID"] = "8." + gkey;
                dwCO["QUARTICLE"] = yart;
                dwCO["QUVAR"] = yvar;
                dwCO["QUCOLOR"] = ycolor;
                dwCO["QUVAR"] = yvar;
                dwCO["QUCOLOR"] = ycolor;
                dwCO["QUDESS"] = ydess;
                dwCO["QUCOLORIT"] = ycolorway;
                dwCO["QUFINISH"] = yfinish;
                dwCO["QUCQUANTITY"] = yqty;
                dwCO["QUCVALUE"] = 0;
                dwCO["QUCPRICE"] = 0;
                dwCO["QURATIO"] = 1;
                tbMaster.Rows.Add(dwCO);
            }

        }
        private void CreateGPrintingCost(DataRow dwInput)
        {
            string vart = "", vvar = "", vcolor = "", vdess = "", vcolorit = "", vfinish = "", order = "", line = "";
            double qty = 0.0;
            double bomratio = 0.0;
            string expression = "";
            DataRow[] rows = null;
            DataRow[] rows1 = null;
            double fnratio = 1;
            string key = order + line;
            string KRS = "", LFD = "", POS = "";

            KRS = dwInput["ABPAUFKRS"].ToString();
            LFD = dwInput["ABPAUFLFD"].ToString();
            POS = dwInput["ABPAUFPOS"].ToString();
            key = "AB" + KRS + LFD + POS;
            qty = System.Convert.ToDouble(dwInput["ABPEAKG"].ToString()) * 0.01;
            vart = dwInput["ABPART"].ToString();
            vvar = dwInput["ABPVAR"].ToString();
            vcolor = dwInput["ABPFARBE"].ToString();
            vdess = dwInput["ABPDESS"].ToString();
            vcolorit = dwInput["ABPCOLORIT"].ToString();
            DataRow dwCO = tbMaster.NewRow();
            dwCO["QUUSER"] = UserID;
            dwCO["QUDATE"] = iDate;
            dwCO["QUID"] = GetQUID();
            dwCO["QUORDER"] = order;
            dwCO["QUCVALUE"] = 0;
            dwCO["QULINE"] = line;
            dwCO["QUSTAGE"] = 7;
            dwCO["QUSTAGEDES"] = "印花";
            dwCO["QUSOURCETYPE"] = "I";
            //自己的ID（BOMID），上一级的ID（SBOMID）
            dwCO["QUBOMID"] = "7." + key;
            dwCO["QUSBOMID"] = "8." + key;
            dwCO["QUARTICLE"] = vart;
            dwCO["QUVAR"] = vvar;
            dwCO["QUCOLOR"] = vcolor;
            dwCO["QUDESS"] = vdess;
            dwCO["QUCOLORIT"] = vcolorit;
            dwCO["QUCQUANTITY"] = qty;
            dwCO["QUCPRICE"] = 0;
            tbMaster.Rows.Add(dwCO);
            /*
                        var query = from dw in SETINTEXDB.WBBEW.AsEnumerable()
                                    where (dw["WBEWBS"] == "48" || dw["WBEWBS"] == "36")
                                    & dw["WBEWAUFKRS"].ToString() == KRS
                                    & dw["WBEWAUFLFD"].ToString() == LFD
                                    & dw["WBEWAUFPOS"].ToString() == POS
                                    group dw by new
                                    {
                                        WBEWART = dw["WBEWART"],
                                        WBEWVAR = dw["WBEWVAR"],
                                        WBEWDESS = dw["WBEWDESS"],
                                        WBEWCOLORIT = dw["WBEWCOLORIT"],
                                        WBEWFARBE = dw["WBEWFARBE"],
                                        WBEWBESCHICH = dw["WBEWBESCHICH"]
                                    }
                                        into g
                                        select new
                                        {
                                            Fabric = g.Key,
                                            TWeight = g.Sum(dwWastga => dwWastga.Field<decimal>("WBEWGEW")),
                                        };
                        */

            var query = from dw in SETINTEXDB.WBBEW.AsEnumerable()
                        where (dw["WBEWBS"].ToString() == "48" || dw["WBEWBS"].ToString() == "36")
                        & dw["WBEWAUFKRS"].ToString() == KRS
                        & dw["WBEWAUFLFD"].ToString() == LFD
                        & dw["WBEWAUFPOS"].ToString() == POS
                        select dw;
            string gkey = key;
            foreach (var row in query)
            {
                string yart = row["WBEWART"].ToString();
                string yvar = row["WBEWVAR"].ToString();
                string ydess = row["WBEWDESS"].ToString();
                string ycolorway = row["WBEWCOLORIT"].ToString();
                string ycolor = row["WBEWFARBE"].ToString();
                string yfinish = row["WBEWBESCHICH"].ToString();
                double yqty = System.Convert.ToDouble(row["WBEWGEW"].ToString()) * 0.001; //   取留位数量

                dwCO = tbMaster.NewRow();
                dwCO["QUUSER"] = UserID;
                dwCO["QUDATE"] = iDate;
                dwCO["QUID"] = GetQUID();
                dwCO["QUORDER"] = order;
                dwCO["QULINE"] = line;
                dwCO["QUSTAGE"] = 733;
                dwCO["QUSTAGEDES"] = "坯布面料";
                dwCO["QUSOURCETYPE"] = "I";
                //自己的ID（BOMID），上一级的ID（SBOMID）
                dwCO["QUBOMID"] = "7." + gkey;
                dwCO["QUSBOMID"] = "7." + gkey;
                dwCO["QUARTICLE"] = yart;
                dwCO["QUVAR"] = yvar;
                dwCO["QUCOLOR"] = ycolor;
                dwCO["QUVAR"] = yvar;
                dwCO["QUCOLOR"] = ycolor;
                dwCO["QUDESS"] = ydess;
                dwCO["QUCOLORIT"] = ycolorway;
                dwCO["QUFINISH"] = yfinish;
                dwCO["QUCQUANTITY"] = yqty;
                dwCO["QUCVALUE"] = 0;
                dwCO["QUCPRICE"] = 0;
                dwCO["QURATIO"] = 1;
                tbMaster.Rows.Add(dwCO);
            }

        }
        private void CreateGDyeingCost(DataRow dwInput)
        {
            string vart = "", vvar = "", vcolor = "", vdess = "", vcolorit = "", vfinish = "", order = "", line = "";
            double qty = 0.0;
            double bomratio = 0.0;
            string expression = "";
            DataRow[] rows = null;
            DataRow[] rows1 = null;
            double fnratio = 1;
            string key = order + line;
            string KRS = "", LFD = "", POS = "";

            KRS = dwInput["ABPAUFKRS"].ToString();
            LFD = dwInput["ABPAUFLFD"].ToString();
            POS = dwInput["ABPAUFPOS"].ToString();
            key = "AB" + KRS + LFD + POS;
            qty = System.Convert.ToDouble(dwInput["ABPEAKG"].ToString()) * 0.01;
            vart = dwInput["ABPART"].ToString();
            vvar = dwInput["ABPVAR"].ToString();
            vcolor = dwInput["ABPFARBE"].ToString();
            DataRow dwCO = tbMaster.NewRow();
            dwCO["QUUSER"] = UserID;
            dwCO["QUDATE"] = iDate;
            dwCO["QUID"] = GetQUID();
            dwCO["QUORDER"] = order;
            dwCO["QUCVALUE"] = 0;
            dwCO["QULINE"] = line;
            dwCO["QUSTAGE"] = 6;
            dwCO["QUSTAGEDES"] = "染色";
            dwCO["QUSOURCETYPE"] = "I";
            //自己的ID（BOMID），上一级的ID（SBOMID）
            dwCO["QUBOMID"] = "6." + key;
            dwCO["QUSBOMID"] = "";
            dwCO["QUARTICLE"] = vart;
            dwCO["QUVAR"] = vvar;
            dwCO["QUCOLOR"] = vcolor;
            dwCO["QUCQUANTITY"] = qty;
            dwCO["QUCPRICE"] = 0;
            tbMaster.Rows.Add(dwCO);
            /*
            var query = from dw in SETINTEXDB.WBBEW.AsEnumerable()
                        where (dw["WBEWBS"] == "48" || dw["WBEWBS"] == "36")
                        & dw["WBEWAUFKRS"].ToString() == KRS
                        & dw["WBEWAUFLFD"].ToString() == LFD
                        & dw["WBEWAUFPOS"].ToString() == POS
                        group dw by new
                        {
                            WBEWART = dw["WBEWART"],
                            WBEWVAR = dw["WBEWVAR"],
                            WBEWDESS = dw["WBEWDESS"],
                            WBEWCOLORIT = dw["WBEWCOLORIT"],
                            WBEWFARBE = dw["WBEWFARBE"],
                            WBEWBESCHICH = dw["WBEWBESCHICH"]
                        }
                            into g
                            select new
                            {
                                Fabric = g.Key,
                                TWeight = g.Sum(dwWastga => dwWastga.Field<decimal>("WBEWGEW")),
                            };            */

            var query = from dw in SETINTEXDB.WBBEW.AsEnumerable()
                        where (dw["WBEWBS"].ToString() == "48" || dw["WBEWBS"].ToString() == "36")
                        & dw["WBEWAUFKRS"].ToString() == KRS
                        & dw["WBEWAUFLFD"].ToString() == LFD
                        & dw["WBEWAUFPOS"].ToString() == POS
                        select dw;
            string gkey = key;
            foreach (var row in query)
            {
                string yart = row["WBEWART"].ToString();
                string yvar = row["WBEWVAR"].ToString();
                string ydess = row["WBEWDESS"].ToString();
                string ycolorway = row["WBEWCOLORIT"].ToString();
                string ycolor = row["WBEWFARBE"].ToString();
                string yfinish = row["WBEWBESCHICH"].ToString();
                double yqty = System.Convert.ToDouble(row["WBEWGEW"].ToString()) * 0.001; //   取留位数量

                dwCO = tbMaster.NewRow();
                dwCO["QUUSER"] = UserID;
                dwCO["QUDATE"] = iDate;
                dwCO["QUID"] = GetQUID();
                dwCO["QUORDER"] = order;
                dwCO["QULINE"] = line;
                dwCO["QUSTAGE"] = 733;
                dwCO["QUSTAGEDES"] = "坯布面料";
                dwCO["QUSOURCETYPE"] = "I";
                //自己的ID（BOMID），上一级的ID（SBOMID）
                dwCO["QUBOMID"] = "6." + gkey;
                dwCO["QUSBOMID"] = "6." + gkey;
                dwCO["QUARTICLE"] = yart;
                dwCO["QUVAR"] = yvar;
                dwCO["QUCOLOR"] = ycolor;
                dwCO["QUVAR"] = yvar;
                dwCO["QUCOLOR"] = ycolor;
                dwCO["QUDESS"] = ydess;
                dwCO["QUCOLORIT"] = ycolorway;
                dwCO["QUFINISH"] = yfinish;
                dwCO["QUCQUANTITY"] = yqty;
                dwCO["QUCVALUE"] = 0;
                dwCO["QUCPRICE"] = 0;
                dwCO["QURATIO"] = 1;
                tbMaster.Rows.Add(dwCO);
            }
        }
        private void CreateGreigeCost(DataRow dwInput)
        {
            string vart = "", vvar = "", vcolor = "", vdess = "", vcolorit = "", vfinish = "", order = "", line = "";
            double qty = 0.0;
            double bomratio = 0.0;
            string expression = "";
            DataRow[] rows = null;
            DataRow[] rows1 = null;
            double fnratio = 1;
            string key = order + line;
            string KRS = "", LFD = "", POS = "";
            //需要判断采购的坯布
            KRS = dwInput["ABPAUFKRS"].ToString();
            LFD = dwInput["ABPAUFLFD"].ToString();
            POS = dwInput["ABPAUFPOS"].ToString();
            key = "AB" + KRS + LFD + POS;
            qty = System.Convert.ToDouble(dwInput["ABPEAKG"].ToString()) * 0.01;
            vart = dwInput["ABPART"].ToString();
            vvar = dwInput["ABPVAR"].ToString();
            vcolor = dwInput["ABPFARBE"].ToString();
            DataRow dwCO = tbMaster.NewRow();
            dwCO["QUUSER"] = UserID;
            dwCO["QUDATE"] = iDate;
            dwCO["QUID"] = GetQUID();
            dwCO["QUORDER"] = order;
            dwCO["QULINE"] = line;
            dwCO["QUCVALUE"] = 0;
            dwCO["QUSTAGE"] = 5;
            dwCO["QUSTAGEDES"] = "织造";
            dwCO["QUSOURCETYPE"] = "I";
            //自己的ID（BOMID），上一级的ID（SBOMID）
            dwCO["QUBOMID"] = "5." + key;
            dwCO["QUSBOMID"] = "6." + key;
            dwCO["QUARTICLE"] = vart;
            dwCO["QUVAR"] = vvar;
            dwCO["QUCOLOR"] = vcolor;
            dwCO["QUCQUANTITY"] = qty;
            dwCO["QUCPRICE"] = 0;
            dwCO["QURATIO"] = 1;
            tbMaster.Rows.Add(dwCO);

            expression = "GAAUFKRS='" + KRS + "' AND GAAUFLFD='" + LFD + "' AND GAAUFPOS='" + POS + "'";
            rows = SETINTEXDB.GAAUF.Select(expression);
            string gkey = key;
            foreach (DataRow row in rows)
            {
                string yart = row["GAART"].ToString();
                string yvar = row["GAVAR"].ToString();
                string ycolor = row["GAFARBE"].ToString();
                double yqty = System.Convert.ToDouble(row["GAKG6"].ToString()) * 0.01;
                expression = "ABPART='" + yart + "' AND ABPVAR='" + yvar + "' AND ABPFARBE='" + ycolor + "'";
                rows1 = SETINTEXDB.ABPOS.Select(expression);
                if (rows1.Length == 0) continue;
                dwCO = tbMaster.NewRow();
                dwCO["QUUSER"] = UserID;
                dwCO["QUDATE"] = iDate;
                dwCO["QUID"] = GetQUID();
                dwCO["QUORDER"] = order;
                dwCO["QULINE"] = line;
                dwCO["QUSTAGE"] = 733;
                dwCO["QUSTAGEDES"] = "色纱";
                dwCO["QUSOURCETYPE"] = "I";
                //自己的ID（BOMID），上一级的ID（SBOMID）
                dwCO["QUBOMID"] = "21." + gkey;
                dwCO["QUSBOMID"] = "5." + gkey;
                dwCO["QUARTICLE"] = yart;
                dwCO["QUVAR"] = yvar;
                dwCO["QUCOLOR"] = ycolor;
                dwCO["QUCQUANTITY"] = yqty;
                dwCO["QUCVALUE"] = 0;
                dwCO["QUCPRICE"] = 0;
                dwCO["QURATIO"] = 1;
                tbMaster.Rows.Add(dwCO);

            }
        }
        private void CreateDyedYarnCost(DataRow dwInput)
        {
            string vart = "", vvar = "", vcolor = "", vdess = "", vcolorit = "", vfinish = "", order = "", line = "";
            double qty = 0.0;
            double bomratio = 0.0;
            string expression = "";
            DataRow[] rows = null;
            DataRow[] rows1 = null;
            double fnratio = 1;
            string key = order + line;
            string KRS = "", LFD = "", POS = "";
            //需要判断采购的坯布
            KRS = dwInput["ABPAUFKRS"].ToString();
            LFD = dwInput["ABPAUFLFD"].ToString();
            POS = dwInput["ABPAUFPOS"].ToString();
            key = "AB" + KRS + LFD + POS;
            qty = System.Convert.ToDouble(dwInput["ABPEAKG"].ToString()) * 0.01;
            vart = dwInput["ABPART"].ToString();
            vvar = dwInput["ABPVAR"].ToString();
            vcolor = dwInput["ABPFARBE"].ToString();
            DataRow dwCO = tbMaster.NewRow();
            dwCO["QUUSER"] = UserID;
            dwCO["QUDATE"] = iDate;
            dwCO["QUID"] = GetQUID();
            dwCO["QUORDER"] = order;
            dwCO["QULINE"] = line;
            dwCO["QUJW"] = "W";
            dwCO["QUSOURCETYPE"] = "I";
            dwCO["QUSTAGE"] = 21;
            dwCO["QUSTAGEDES"] = "色纱";
            dwCO["QUBOMID"] = "21." + key;
            //自己的ID（BOMID），上一级的ID（SBOMID）                    
            dwCO["QUSBOMID"] = "5." + key;
            dwCO["QUARTICLE"] = vart;
            dwCO["QUVAR"] = vvar;
            dwCO["QUCOLOR"] = vcolor;
            dwCO["QURATIO"] = 1;
            dwCO["QUCVALUE"] = 0;
            dwCO["QUCPRICE"] = 0;
            dwCO["QUCQUANTITY"] = qty;
            tbMaster.Rows.Add(dwCO);
        }
        private void CreateMatPrice(DataRow dwRYarn, DataRow dwEKPOS, string mtype = "Yarn")
        {
            string vart = "", vvar = "", vcolor = "", vdess = "", vcolorit = "", vfinish = "", order = "", line = "";
            double qty = 0.0;
            double bomratio = 0.0;
            string expression = "";
            DataRow[] rows = null;
            DataRow[] rows1 = null;
            double fnratio = 1;
            string key = "";
            string KRS = "", LFD = "", POS = "", Line = "";
            string gasa = "";
            DataRow dwCO = null;
            switch (mtype)
            {
                case "Yarn":
                    //需要判断采购的坯布
                    KRS = dwRYarn["GAAUFKRS"].ToString();
                    LFD = dwRYarn["GAAUFLFD"].ToString();
                    POS = dwRYarn["GAAUFPOS"].ToString();
                    Line = dwRYarn["GAPOS"].ToString();
                    gasa = dwRYarn["GASA"].ToString();
                    key = "AB" + KRS + LFD + POS;
                    qty = System.Convert.ToDouble(dwRYarn["GAKG6"].ToString()) * 0.01;
                    vart = dwRYarn["GAART"].ToString();
                    vvar = dwRYarn["GAVAR"].ToString();
                    vcolor = dwRYarn["GAFARBE"].ToString();
                    dwCO = tbMaster.NewRow();
                    dwCO["QUUSER"] = UserID;
                    dwCO["QUDATE"] = iDate;
                    dwCO["QUID"] = GetQUID();
                    dwCO["QUORDER"] = order;
                    dwCO["QULINE"] = line;
                    dwCO["QUJW"] = "W";
                    dwCO["QUSOURCETYPE"] = "P";
                    dwCO["QUSTAGE"] = "EK";
                    dwCO["QUSTAGEDES"] = "原纱";
                    dwCO["QUBOMID"] = "EK." + key + Line;
                    //自己的ID（BOMID），上一级的ID（SBOMID）      
                    dwCO["QUSBOMID"] = "21." + key;
                    dwCO["QUARTICLE"] = vart;
                    dwCO["QUVAR"] = vvar;
                    dwCO["QUCOLOR"] = vcolor;
                    dwCO["QURATIO"] = 1;
                    dwCO["QUCQUANTITY"] = qty;
                    dwCO["QUCPRICE"] = 0;
                    dwCO["QUCVALUE"] = 0;

                    if (dwEKPOS != null)
                    {
                        double price = System.Convert.ToDouble(dwEKPOS["EKPPRME"].ToString()) * 0.001;
                        double value = qty * price;
                        dwCO["QUCPRICE"] = price;
                        dwCO["QUCVALUE"] = 0;
                    }
                    tbMaster.Rows.Add(dwCO);
                    break;
                case "Greige":
                    //需要判断采购的坯布
                    KRS = dwRYarn["GAAUFKRS"].ToString();
                    LFD = dwRYarn["GAAUFLFD"].ToString();
                    POS = dwRYarn["GAAUFPOS"].ToString();
                    Line = dwRYarn["GAPOS"].ToString();
                    gasa = dwRYarn["GASA"].ToString();
                    key = "AB" + KRS + LFD + POS;
                    qty = System.Convert.ToDouble(dwRYarn["GAKG6"].ToString()) * 0.01;
                    vart = dwRYarn["GAART"].ToString();
                    vvar = dwRYarn["GAVAR"].ToString();
                    vcolor = dwRYarn["GAFARBE"].ToString();
                    dwCO = tbMaster.NewRow();
                    dwCO["QUUSER"] = UserID;
                    dwCO["QUDATE"] = iDate;
                    dwCO["QUID"] = GetQUID();
                    dwCO["QUORDER"] = order;
                    dwCO["QULINE"] = line;
                    dwCO["QUJW"] = "W";
                    dwCO["QUSOURCETYPE"] = "P";
                    dwCO["QUSTAGE"] = "EK";
                    dwCO["QUSTAGEDES"] = "原纱";
                    dwCO["QUBOMID"] = "EK." + key + Line;
                    dwCO["QUSBOMID"] = "5." + key;
                    dwCO["QUARTICLE"] = vart;
                    dwCO["QUVAR"] = vvar;
                    dwCO["QUCOLOR"] = vcolor;
                    dwCO["QURATIO"] = 1;
                    dwCO["QUCQUANTITY"] = qty;
                    dwCO["QUCPRICE"] = 0;
                    dwCO["QUCVALUE"] = 0;
                    if (dwEKPOS != null)
                    {
                        double price = System.Convert.ToDouble(dwEKPOS["EKPPRME"].ToString()) * 0.001;
                        double value = qty * price;
                        dwCO["QUCPRICE"] = price;
                        dwCO["QUCVALUE"] = 0;
                    }
                    tbMaster.Rows.Add(dwCO);
                    break;
                default:
                    break;
            }

        }


    }
}
